<?php

namespace app\zmt\controller;

use app\zmt\model\GoodsModel;
use cmf\controller\AdminBaseController;
use think\facade\Db;
use think\Request;

class GoodsController extends AdminBaseController
{
    public function index(Request $request)
    {
        $select = $request->param();
        $where = [];
        if($select){
            $where[] = ['name|ziying|state','like',"%$select%"];
        }
        $res = GoodsModel::order('id','desc')
            ->where($where)
            ->limit(5)
            ->paginate();
        $this->assign('list',$res);
        return $this->fetch();
    }

    //添加商品
    public function add()
    {
        return $this->fetch();
    }

    //提交添加
    public function addPost(Request $request)
    {
        $data = $request->param();
        $data['fileall'] = implode(',',$data['fileall']);
        if(empty($data['file']))
        {
            $this->error('图片不能为空');
        }
        if(empty($data['fileall']))
        {
            $this->error('图片不能为空');
        }
        if(empty($data['name']))
        {
            $this->error('图片名称不能为空');
        }
        $data['file'] = cmf_get_image_url($data['file']);
        $data['create_time'] = date('Y:m:d H:i:s');
        $res = GoodsModel::create($data);
        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }

    //是否自营修改
    public function show(Request $request)
    {
        $id = $request->param('id');
        if(empty($id))
        {
            $this->error('修改失败');
        }
        $res = GoodsModel::where('id','=',$id)
            ->find();
        if($res['ziying'] == 0)
        {
            GoodsModel::where('id','=',$id)
                ->update(['ziying'=>1]);
            $this->success('修改成功');
        }else{
            GoodsModel::where('id','=',$id)
                ->update(['ziying'=>0]);
            $this->success('修改成功');
        }
    }

    //修改商品
    public function edit(Request $request)
    {
        $id = $request->param('id');
        if(empty($id))
        {
            $this->error('修改失败');
        }
        $res = GoodsModel::where('id','=',$id)
            ->find();
        $this->assign('res',$res);
        return $this->fetch();
    }

    //提交修改
    public function editPost(Request $request)
    {
        $data = $request->param();
        if(!$data['id'])
        {
            $this->error('参数错误');
        }
        if(!$data['fileall'])
        {
            $this->error('缺少参数');
        }
        if(!$data['name'])
        {
            $this->error('缺少参数');
        }
        $data['update_time'] = date('Y:m:d H:i:s');
        $res = GoodsModel::where('id','=',$data['id'])
            ->update($data);
        if($res)
        {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    //删除商品
    public function delete(Request $request)
    {
        $id = $request->param('id/d');
        if(!$id)
        {
            $this->error('参数错误');
        }
        $res = GoodsModel::where('id','=',$id)
            ->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //打开配置页面
    public function setGoodsAttr()
    {
        $goodsId = input('goods_id');
        $this->assign('goods_id', $goodsId);
        return $this->fetch('add_goods_attr');
    }

    //配置提交
    public function setGoodsAttrPost()
    {
        $attrs = input('attr');
        $goodsId = input('goods_id');
        if (count($attrs) < 1) {
            $this->error('请至少配置一个属性！');
        }
        GoodsModel::where('goods_id','=',$goodsId)
            ->delete();
        $insert_data = [];
        foreach ($attrs as $attr) {
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr['name']
            ];
        }
        $res = Db::name('goods_attr')
            ->insertAll($insert_data);
        if ($res) {
            $this->success("操作成功");
        }
    }

    //打开配置商品属性值页面
    public function setGoodsAttrValue()
    {
        $goodsId = input('goods_id');
        $this->assign('goods_id', $goodsId);
        return $this->fetch('add_goods_attr_value');
    }

    //提交表单
    public function setGoodsAttrValuePost()
    {
        halt(input());
    }

    //查询某个商品有哪些属性值
    public function getGoodsAttr()
    {
        $goodsId = input('goods_id');
        //查询商品的属性值
        $attrs = Db::name('goods_attr')
            ->where('goods_id', $goodsId)
            ->select();
        return json(['code' => 1, 'msg' => '商品属性', 'data' => $attrs]);
    }

    public function setAttr()
    {
        $goods_id = input('goods_id');
        $this->assign('goods_id',$goods_id);
        return $this->fetch();
    }
    public function AttrIndex()
    {
        $goods_id = input('goods_id');
        $res = Db::name('product_attr')
            ->where('goods_id','=',$goods_id)
            ->select();
        return json(['code'=>1, 'msg'=>'', 'data'=>$res]);
    }

    public function sentAttrIndex()
    {
        $attr= input('attr');
        $goods_id = input('goods_id');
        $data = [];
        foreach ($attr as $attrs){
            $data[] = [
                'goods_id'=>$goods_id,
                'attr_id'=>$attrs['attr'],
                'attr_value'=>$attrs['attr_value']
            ];
        }
        Db::name('product_attr')
            ->insertAll($data);
    }
    public function setGoods()
    {
        $goods_id = input('goods_id');
        $this->assign('goods_id',$goods_id);
        return $this->fetch();
    }
    public function GoodsAttrGet()
    {
        $attr = input('attr');
        $goods_id = input('goods_id');
        $attr = json_encode($attr,JSON_UNESCAPED_UNICODE);
        $res = GoodsModel::where('id','=',$goods_id)
            ->update(['attr'=>$attr]);
        if($res)
        {
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function GoodsAttrIndex()
    {
        $goods_id = input('goods_id');
        $res = GoodsModel::find($goods_id);
        $res['attr'] = json_decode($res['attr'],true);
        return json(['code'=>1,'msg'=>'','data'=>$res['attr']]);
    }
}